function $(s){
	return document.getElementById(s);
}
// 根据class得到元素
function $class(s) {
	return document.getElementsByClassName(s);
}

(function(Play){
	
	function SlidePanel(){
		
		//屏幕宽高信息
		var winW = window.innerWidth;
		var winH = window.innerHeight;
		//检查是否支持动画
		var has3d = (navigator.appVersion.search(/android/i) == -1);
		//移动对象
		var moveObj = {
			moveEle : null,//移动对象dom元素
			boxList:[],
			offset : {		//移动位置
				x:0,
				y:0
			},
			endOffset:{
				x:0,
				y:0
			},
			currIndex:0
		};

		//移动方向
		var SLIDE_TYPE = {
			top:1,
			right:2,
			bottom:3,
			left:4
		}
		
		var touchInfo = {
			orgP : {
				x : 0,
				y:0
			},
			aimP : {
				x : 0,
				y:0
			},
			moved :{
				x:0,
				y:0
			},
			setOrgP :function(data){
				this.orgP.x = data.x;
				this.orgP.y = data.y;
			},
			setAimP :function(data){
				this.aimP.x = data.x;
				this.aimP.y = data.y;
			},
			dealMoved:function(){
				this.moved.x = this.aimP.x-this.orgP.x;
				this.moved.y = this.aimP.y-this.orgP.y;
			}
		}
		
		
		/*-------------------------------内部方法  ---------------------------*/
		
		
		function _init(element){
			//得到滑动对象
			var panelDom = $(element);
			moveObj.moveEle = panelDom.firstElementChild;
			//初始化容器尺寸
			_initSize(panelDom);
			if(!moveObj.moveEle){
				console.log("移动元素未得到");
			}
			
			_initTouchEvent();
		}
		
		function _initSize(panelDom){
			panelDom.style.width = winW+"px";
			//panelDom.style.height = winH+"px";
			var allBoxs = panelDom.firstElementChild.children;
			for(var i=0,n=allBoxs.length;i<n;i++){
				allBoxs[i].style.width = winW+"px";
				moveObj.boxList.push(allBoxs[i]);
			}
		}
			// 添加touch事件
		var _initTouchEvent = function() {
			
			// 位置基本信息
			
			document.addEventListener("touchstart",touchStar, false);
	
			document.addEventListener("touchmove",touchMove, false);
	
			document.addEventListener("touchend", touchEnd, false);
	
		}
		
		function getTouchPostionInfo(touch){
			if(touch.screenX){
				return {x:touch.screenX,y:touch.screenY};
			}else{
			
				return {x:touch.pageX,y:touch.pageY};
			}
		}
		
		function touchStar(event){
			var touch = event.touches[0];
			// 设置源对象位置
			touchInfo.setOrgP(getTouchPostionInfo(touch));
			
		}
		
		//更新移动信息
		function updateMoveInfo(touch){
			touchInfo.setAimP(getTouchPostionInfo(touch));
			touchInfo.dealMoved();
			
		}
		function updatePostion(){
			var move = {};
			move.x = moveObj.endOffset.x +touchInfo.moved.x;
			move.y = moveObj.endOffset.y +touchInfo.moved.y;
			_translate(move.x,move.y);
		
		}
		function touchMove(event){
			event.preventDefault();
			var touch = event.touches[0];
			updateMoveInfo(touch);
			
			updatePostion();
		
		}
		
		function updateEnd(){
			var move = {};
				
			if(touchInfo.moved.x>0){
				console.log("---->");
				if(moveObj.currIndex!=0){
					moveObj.currIndex--;
				}
			}else if(touchInfo.moved.x<0){
				if(moveObj.currIndex!=(moveObj.boxList.length-1)){
					moveObj.currIndex++;
				}
				console.log("<----");
			}
			console.log(moveObj.currIndex);
			move.x = -moveObj.currIndex*winW;
			move.y = -moveObj.currIndex*winH;
			
			_translate(move.x,move.y);
			console.log(move.x);
			moveObj.endOffset.x = move.x; 
			moveObj.endOffset.y = move.y; 
		}
		function touchEnd(event){
			updateEnd();			
		}
		
	    function _translate (x,y) {
	    	if(isNaN(x)||isNaN(y)){
	    		console.log("x,y不同为数字");
	    	}
	    	var x = x||0;
	    	var y = y||0;
			if (has3d) {
				moveObj.moveEle.style.webkitTransform = 'translate3d(' + x+ 'px,' + 0+ 'px,0px)';
			} else {
				moveObj.moveEle.style.webkitTransform = 'translate(' + x + 'px,' + 0 + 'px)';
			}
			
			
		}
		
		/*-------------------------------内部方法 /  ---------------------------*/
		
		
		/*-------------------------------对外公开调用方法  ---------------------------*/
		
		/**
		 * 滑动
		 */
		this.slide = function(slideType){
		
		}
	
	
		this.init = function(element){
			_init(element);
		}
		
		//transform 性测试
		this.testTransform = function(){
			var flag = -1;
			setInterval(function(){
				_translate (-1*flag*winW,-Math.random()*300);
				flag = -1*flag;
			},1000)
		}
		/*-------------------------------对外公开调用方法  /---------------------------*/
		
		return this;
	}
	Play.slide = SlidePanel;
	
})(Play);

